//package com.zpz.framework.zpzoauth.config.oauth.authorization.exouath.authenticator.partner.zhy;
//
//import com.alibaba.fastjson.JSON;
//import SignUtil;
//import com.zpz.framework.zpzoauth.common.encrypt.sm4.Sm4Utils;
//import ZpzOauthResultMsg;
//import ZpzOauthResultStatus;
//import ZpzOauthHttpUtil;
//import ZpzOauthIntegrationAuthentication;
//import AbstractPreparableIntegrationAuthenticator;
//import ModifyUserLoginDateQo;
//import UserAuthenticationVo;
//import ZpzOauthFrameUserService;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.http.client.utils.DateUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Primary;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
//import org.springframework.stereotype.Component;
//
//import java.util.Date;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @author zhangpanzhi
// * @time 2019-07-09
// * @description 未经本人允许请勿随便改动，尊重劳动
// * */
//@Component
//@Primary
//public class ZpzOauthAppUserNameLoginAuthenticator extends AbstractPreparableIntegrationAuthenticator {
//    private Logger log = LoggerFactory.getLogger(this.getClass());
//    @Autowired
//    private PasswordEncoder passwordEncoder;
//    @Autowired
//    private ZpzOauthFrameUserService zpzOauthFrameUserService;
//    @Value("${zpzOauth.partner}")
//    private String partner;
//    private final static String U_P_AUTH_TYPE = "zhy_app_login";
//    @Override
//    public UserAuthenticationVo authenticate(ZpzOauthIntegrationAuthentication zpzOauthIntegrationAuthenticator) {
//        log.info("中海油APP登录：入参："+ JSON.toJSONString(zpzOauthIntegrationAuthenticator));
//        Map<String,Object> partnerStr = (Map<String, Object>) JSON.parse(partner);
//        Object zhy = partnerStr.get("zhy");
//        if (zhy==null){
//            throw new OAuth2Exception("登录授权失败[ZPZ_DEFINED_OAUTH2_EXCEPTION]["+ ZpzOauthResultStatus.OAUTH_10008.getCode() +"]：配置错误");
//        }
//        Map<String,Object> zhyStr = (Map<String, Object>) JSON.parse(zhy.toString());
//        String appSmsCodeUrl = (String)zhyStr.get("appSmsCodeUrl");
//        String appAppId = (String)zhyStr.get("appAppId");
//        String operator = (String)zhyStr.get("operator");
//        String authKey = (String)zhyStr.get("authKey");
//        String appBaowenJiaKey = (String)zhyStr.get("appBaowenJiaKey");
//        String appSignKey = (String)zhyStr.get("appSignKey");
//        String appSm4Key = (String)zhyStr.get("appSm4Key");
//        String timestamp = DateUtils.formatDate(new Date(), "yyyyMMddHHmmss");
//        String nonceStr = SignUtil.generateNonceStr(8);
//        //请求参数
//        Map<String,Object> param=new HashMap<>();
//        param.put("appId",appAppId);
//        param.put("requestType","1");
//        param.put("epsessionId","");
//        param.put("authType","sms");
//        Map<String,String> authPara=new HashMap<>();
//        authPara.put("mobileNum",zpzOauthIntegrationAuthenticator.getUsername());
//        String password = zpzOauthIntegrationAuthenticator.getAuthParameter("password");
//        authPara.put("smsCode",password);
//        param.put("authPara",authPara);
//        param.put("device","app");
//        param.put("hostname","app");
//        //头部参数
//        Map<String,String> header=new HashMap<>();
//        header.put("operator",operator);
//        header.put("randomstr", nonceStr);
//        header.put("timestamp", timestamp);
//        try {
//            header.put("encode", SignUtil.MD5(operator + nonceStr + timestamp+authKey));
//        } catch (Exception e) {
//            throw new OAuth2Exception("登录授权失败[ZPZ_DEFINED_OAUTH2_EXCEPTION]["+ ZpzOauthResultStatus.OAUTH_10008.getCode() +"]：配置错误");
//        }
//        Sm4Utils sm4Utils = new Sm4Utils();
//        String enParam =sm4Utils.encryptData_ECB(JSON.toJSONString(param), appBaowenJiaKey);
//        Map<String,String> body=new HashMap<>();
//        body.put("u",enParam);
//        try {
//            header.put("sign", SignUtil.MD5(appSignKey+JSON.toJSONString(body)));
//        } catch (Exception e) {
//            throw new OAuth2Exception("登录授权失败[ZPZ_DEFINED_OAUTH2_EXCEPTION]["+ ZpzOauthResultStatus.OAUTH_10008.getCode() +"]：配置错误");
//        }
//        String res = null;
//        try {
//            res = ZpzOauthHttpUtil.getInstance().doPostJsonString(appSmsCodeUrl,null, JSON.toJSONString(body),header);
//        } catch (Exception e) {
//            throw new OAuth2Exception("登录授权失败[ZPZ_DEFINED_OAUTH2_EXCEPTION]["+ ZpzOauthResultStatus.OAUTH_10008.getCode() +"]：配置错误");
//        }
//        if (StringUtils.isNotBlank(res)){
//            Map<String,Object>resMap=(Map<String, Object>) JSON.parse(res);
//            String u = resMap.get("u").toString();
//            String s = sm4Utils.decryptData_ECB(u, appBaowenJiaKey);
//            Map<String,Object> sMap=(Map<String, Object>) JSON.parse(s);
//            if (sMap.get("code").toString().equals("info.common.success")){
//                ZpzOauthResultMsg<UserAuthenticationVo> resultMsg = zpzOauthFrameUserService.findUserByPhoneNumber(zpzOauthIntegrationAuthenticator.getUsername(), zpzOauthIntegrationAuthenticator.getClientId());
//                if (resultMsg!=null&&resultMsg.getCode()==0){
//                    UserAuthenticationVo data = resultMsg.getData();
//                    data.setPassword(passwordEncoder.encode(password));
//                    ModifyUserLoginDateQo t=new ModifyUserLoginDateQo();
//                    t.setUserCode(data.getUserCode());
//                    t.setClientId(zpzOauthIntegrationAuthenticator.getClientId());
//                    t.setLoginDate(new Date());
//                    ZpzOauthResultMsg<Boolean> flag = zpzOauthFrameUserService.modifyUserLoginDate(t);
//                    log.info("::::::::::修改最新登录时间："+JSON.toJSONString(flag));
//                    return data;
//                }
//            }
//        }
//        return null;
//    }
//    @Override
//    public void prepare(ZpzOauthIntegrationAuthentication integrationAuthentication) {
//
//    }
//
//    @Override
//    public boolean support(ZpzOauthIntegrationAuthentication integrationAuthentication) {
//    	//增加空判断，支持授权码模式
//        return StringUtils.isEmpty(integrationAuthentication.getAuthType())
//        		||U_P_AUTH_TYPE.equals(integrationAuthentication.getAuthType());
//    }
//}
